<template>
    <div class="printer-container">
        <el-card>
            <template #header>
                <div class="card-header">
                    <span>小票打印机管理</span>
                    <el-button type="primary" @click="showAddDialog">添加打印机</el-button>
                </div>
            </template>

            <el-table :data="printerList" style="width: 100%">
                <el-table-column prop="id" label="ID" width="60" />
                <el-table-column prop="name" label="打印机名称" width="150" />
                <el-table-column prop="type" label="类型" width="100">
                    <template #default="scope">
                        <el-tag v-if="scope.row.type === 'feie'" type="success">飞鹅打印机</el-tag>
                        <el-tag v-else-if="scope.row.type === 'yilianyun'" type="primary">易联云</el-tag>
                        <el-tag v-else>其他</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="printer_sn" label="设备号" width="180" />
                <el-table-column prop="status" label="状态" width="80">
                    <template #default="scope">
                        <el-tag v-if="scope.row.status === 1" type="success">启用</el-tag>
                        <el-tag v-else type="danger">禁用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_default" label="默认" width="80">
                    <template #default="scope">
                        <el-tag v-if="scope.row.is_default === 1" type="warning">默认</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="create_time" label="创建时间" width="160" />
                <el-table-column label="操作" fixed="right" width="300">
                    <template #default="scope">
                        <el-button size="small" @click="editPrinter(scope.row)">编辑</el-button>
                        <el-button size="small" type="success" @click="testPrint(scope.row.id)">测试打印</el-button>
                        <el-button size="small" type="warning" @click="setDefault(scope.row.id)" 
                            v-if="scope.row.is_default !== 1">设为默认</el-button>
                        <el-button size="small" type="danger" @click="deletePrinter(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 添加/编辑对话框 -->
        <el-dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
            <el-form :model="formData" label-width="120px">
                <el-form-item label="打印机名称" required>
                    <el-input v-model="formData.name" placeholder="请输入打印机名称" />
                </el-form-item>
                <el-form-item label="打印机类型" required>
                    <el-select v-model="formData.type" placeholder="请选择打印机类型">
                        <el-option label="飞鹅打印机" value="feie" />
                        <el-option label="易联云" value="yilianyun" />
                    </el-select>
                </el-form-item>
                <el-form-item label="设备号(SN)" required>
                    <el-input v-model="formData.printer_sn" placeholder="请输入设备号" />
                </el-form-item>
                <el-form-item label="打印机密钥">
                    <el-input v-model="formData.printer_key" placeholder="请输入密钥" />
                </el-form-item>
                <el-form-item label="API地址">
                    <el-input v-model="formData.api_url" placeholder="默认使用官方接口" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="设为默认">
                    <el-switch v-model="formData.is_default" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="savePrinter">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';

const VITE_URL = window.VITE_URL;

const printerList = ref<any[]>([]);
const dialogVisible = ref(false);
const dialogTitle = ref('添加打印机');
const formData = ref({
    id: 0,
    name: '',
    type: 'feie',
    printer_sn: '',
    printer_key: '',
    api_url: '',
    status: 1,
    is_default: 0
});

// 获取打印机列表
const getPrinterList = () => {
    const url = VITE_URL + '/admin.printer/index';
    axios.get(url).then(res => {
        if (res.data.status === 0) {
            printerList.value = res.data.list;
        }
    });
};

// 显示添加对话框
const showAddDialog = () => {
    dialogTitle.value = '添加打印机';
    formData.value = {
        id: 0,
        name: '',
        type: 'feie',
        printer_sn: '',
        printer_key: '',
        api_url: '',
        status: 1,
        is_default: 0
    };
    dialogVisible.value = true;
};

// 编辑打印机
const editPrinter = (row: any) => {
    dialogTitle.value = '编辑打印机';
    formData.value = { ...row };
    dialogVisible.value = true;
};

// 保存打印机
const savePrinter = () => {
    if (!formData.value.name || !formData.value.printer_sn) {
        ElMessage.error('请填写完整信息');
        return;
    }

    const url = formData.value.id > 0 
        ? VITE_URL + '/admin.printer/edit' 
        : VITE_URL + '/admin.printer/add';

    axios.post(url, formData.value).then(res => {
        if (res.data.status === 0) {
            ElMessage.success(res.data.description || '操作成功');
            dialogVisible.value = false;
            getPrinterList();
        } else {
            ElMessage.error(res.data.description || '操作失败');
        }
    });
};

// 设置默认打印机
const setDefault = (id: number) => {
    const url = VITE_URL + '/admin.printer/set_default';
    axios.post(url, { id }).then(res => {
        if (res.data.status === 0) {
            ElMessage.success('设置成功');
            getPrinterList();
        } else {
            ElMessage.error(res.data.description || '设置失败');
        }
    });
};

// 删除打印机
const deletePrinter = (id: number) => {
    ElMessageBox.confirm('确定要删除这台打印机吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        const url = VITE_URL + '/admin.printer/delete';
        axios.post(url, { id }).then(res => {
            if (res.data.status === 0) {
                ElMessage.success('删除成功');
                getPrinterList();
            } else {
                ElMessage.error(res.data.description || '删除失败');
            }
        });
    });
};

// 测试打印
const testPrint = (id: number) => {
    const url = VITE_URL + '/admin.printerTest/test';
    axios.post(url, { printer_id: id }).then(res => {
        if (res.data.status === 0) {
            ElMessage.success(res.data.description || '测试打印成功，请查看打印机输出');
        } else {
            ElMessage.error(res.data.description || '测试打印失败');
        }
    }).catch(err => {
        ElMessage.error('请求失败：' + err.message);
    });
};

onMounted(() => {
    getPrinterList();
});
</script>

<style scoped>
.printer-container {
    padding: 20px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>
